package com.sz.stock.controller;

import com.sz.stock.client.TUserClient;
import com.sz.stock.model.*;
import com.sz.stock.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/buy")
public class BuyController {

    /**
     * 买卖盘 Service
     */
    @Autowired
    private IBuyOrSaleService iBuyOrSaleService;

    /**
     * 交易系统 Service
     */
    @Autowired
    private ITradingService iTradingService;

    /**
     * 成交Service
     */
    @Autowired
    private IDealService iDealService;

    /**
     * 委托Service
     */
    @Autowired
    private IEntrustService iEntrustService;

    /**
     * 持仓Service
     */
    @Autowired
    private IPosService iPosService;

    @Autowired
    private TUserClient tUserClient;


    /**
     * 1. 添加委托信息. 添加买卖盘信息
     */
    @RequestMapping(value = "/addEntrust", method = RequestMethod.POST)
    public ResultBean addEntrustForBuy(@RequestBody(required = false) Entrust entrust) {
        String data = null;
        // 查询 银行卡信息
        Bankcard bankCard = selectBankCardByUserID(entrust.getStockEntrustUserid());
        ResultBean resultBean = iTradingService.addEntrust(entrust,bankCard);
        if (resultBean.getCode() == 1 && iBuyOrSaleService.addOrUpdate(entrust) > 0) {

            return new ResultBean(101, "添加至委托成功!", resultBean.getData());
        } else {
            data = "添加失败";
            return new ResultBean(444, "添加至委托失败!", data);
        }
    }

    /**
     * 2. 查询 卖出 委托信息(判断是否可以成交)
     */
    @RequestMapping(value = "/selectSellEntrust", method = RequestMethod.POST)
    public ResultBean selectSellEntrust(@RequestBody Entrust entrust) {
        List<Entrust> Sellentrust = iEntrustService.selectSellEntrust(entrust);
        if (Sellentrust.size() == 0) {
            return new ResultBean(0, "查询卖出委托信息失败!", null);
        } else {
            return new ResultBean(Sellentrust.size(), "查询卖出委托信息成功!", Sellentrust);
        }
    }

    /**
     * 3. 将交易成功的买入委托加入到持仓表中
     */
    @RequestMapping(value = "/addPOSForBuy", method = RequestMethod.POST)
    public ResultBean addPosForBuy(@RequestBody(required = false) POS pos) {
        if (iPosService.posAdd(pos) > 0) {
            return new ResultBean(1, "存入持仓成功", 111);
        } else {
            return new ResultBean(0, "存入持仓失败!", null);
        }
    }

    /**
     * 4. 查询 指定股票的 持仓信息
     */
    @RequestMapping(value = "/getOnePos", method = RequestMethod.POST)
    public POS getOnePos(String code, Integer userId) {
        if (iPosService.getOnePos(code, userId) == null) {
            return null;
        } else {
            POS onePos = iPosService.getOnePos(code, userId);
            return onePos;
        }
    }

    /**
     * 5.查询 根据委托ID查询一条委托信息
     */
    @RequestMapping("/selectEntrustById")
    public Entrust selectEntrustById(Integer stockEntrustId) {
        if (iEntrustService.getOneEntrust(stockEntrustId) == null) {
            return null;
        } else {
            return iEntrustService.getOneEntrust(stockEntrustId);
        }
    }

    /**
     * 6.添加 成交
     */
    @RequestMapping(value = "/addDeal", method = RequestMethod.POST)
    public ResultBean addDeal(@RequestBody(required = false) Deal deal) {
        if (iDealService.addDeal(deal) > 0) {
            return new ResultBean(1, "添加成交成功!", "成交成功!");
        } else {
            return new ResultBean(0, "添加成交失败!", "成交失败!");
        }
    }

    /**
     * 7. 修改委托信息
     */
    @RequestMapping(value = "/updateEntrust",method = RequestMethod.POST)
    public Integer updateEntrust(@RequestBody(required = false) Entrust entrust){
        iEntrustService.updateStatus(entrust);
        return null;
    }


    /**
     * 8. 修改持仓信息
     */
    @RequestMapping(value = "/updatePos",method = RequestMethod.POST)
    public Integer updatePos(@RequestBody(required = false) POS pos){
        iPosService.posUpdate(pos);
        return null;
    }

    /**
     * 9. 通过合同编号查询委托信息(成交用)
     */
    @RequestMapping(value = "/selectEntrustByContractID")
    public Entrust selectEntrustByContractID(@RequestParam("contractID") String contractID){
        return iEntrustService.getEntrustByContract(contractID);
    }

    /**
     *  10. 通过用户id 查询银行卡信息
     */
    @RequestMapping("/user/selectBankCardByUserID")
    public Bankcard selectBankCardByUserID(@RequestParam(value = "userid" ,required = false) int userid){
       return tUserClient.selectBankCardByUserID(userid);
    }

    /**
     *  11. 通过 委托查询 买卖盘信息
     */
    @RequestMapping("/selectBos")
    public StockBuyOrSale selectBos(@RequestBody Entrust entrust){
        StockBuyOrSale buyOrSale = new StockBuyOrSale();
        String code = entrust.getStockEntrustCode();
        String type = entrust.getStockEntrustOperate();
        BigDecimal price = entrust.getStockEntrustMoney();
        List<StockBuyOrSale> Bos = iBuyOrSaleService.search(code, type, price);
        for (StockBuyOrSale bo : Bos) {
            buyOrSale = bo;
        }
        return buyOrSale;
    }

    /**
     * 12. 修改买卖盘信息
     */
    @RequestMapping("/updateBos")
    public ResultBean updateBos(@RequestBody StockBuyOrSale stockBuyOrSale){
        Integer i = iBuyOrSaleService.updateBos(stockBuyOrSale);
        if (i>0){
            return new ResultBean(1,"修改成功!",i);
        }else {
            return new ResultBean(0,"修改失败!",null) ;
        }

    }



}
